<template>
	<view class="wrap" style="padding-bottom: 120px;">
		<view class="example-body">
			<view>

					<!-- <u-form-item label="库房" label-width="170">
						<uni-data-select placeholder="请选择库房"
						class="my-input"
						v-model="form.kuf_no" :localdata="kufList"
							@change="changeKuf"></uni-data-select>
					</u-form-item>
					<u-form-item label="区域" label-width="170">
						<uni-data-select placeholder="请选择区域"
						class="my-input"
						:disabled="notChangeQuyNo" v-model="form.quy_no" :localdata="quNoList"
							@change="changeQuyNo"></uni-data-select>
					</u-form-item> -->
					<u-form-item label="托盘号" label-width="170">
						<u-input v-model="form.tpbar"
						class="my-input"
						placeholder="请输入托盘号"
						@click="changeScanInput('form.tpbar')"
						@blur="blurFunction"></u-input>

							<view><u-button class="more-button"
							shape="circle" @click="searchHz">搜索</u-button>
							</view>

					</u-form-item>
					<u-form-item label="点位码" label-width="170">
						<u-input
						class="my-input"
						 @click="changeScanInput('form.landpoint')"
						 placeholder="请输入点位码"
						 v-model="form.landpoint" @blur="blurFunction"></u-input>
					</u-form-item>
					<!-- <u-form-item label="空托数量" label-width="170">
						<u-input
						class="my-input"
						 @click="changeScanInput('form.tpNum')" placeholder="请输入空托数量" type="number"
						 v-model="form.tpNum" @blur="blurFunction"></u-input>
					</u-form-item> -->
					<!-- <u-form-item label="空托数量" label-width="170">
						<uni-data-select placeholder="请选择空托数量"
						class="my-input"
						v-model="form.tpNum" :localdata="rangeNum"
							@change="changeTpNum"></uni-data-select>
					</u-form-item> -->



				<u-empty v-if="!hzList.length" text="无明细数据,请尝试不同的搜索条件进行搜索" mode="page"></u-empty>
				<u-card v-for="(item, index) in hzList" class="task-list-item" :border="false" padding="20" margin="20rpx"
					@click="goTo(item,index)">
					<view slot="head" style="display: flex;align-items: center;justify-content: space-between;">
						<view style="display: flex;align-items: center;font-size: 30rpx;">
							<!-- <image class="user-images" src="/static/aidex/images/user06.png"></image> -->
							{{index+1}}.货位：{{item.LOCATION}}
						</view>
						<view style="color: #999999;font-size: 22rpx;">{{item.YEZ_NAME}}/{{item.YEZ_ID}}</view>
					</view>
					<view class="" slot="body">
						<u-row gutter="16">
							<!-- <u-col span="12">
								<view class="apply-text"><span>托盘号：</span>{{item.TPBAR}}</view>
							</u-col> -->
							<u-col span="12">
								<view class="apply-text"><span>商品：</span>{{item.GDNAME}}</view>
							</u-col>
							<u-col span="12">
								<view class="apply-text"><span>编号：</span>{{item.GDSEQ}}</view>
							</u-col>
							<u-col span="12">
								<view class="apply-text"><span>数量：</span>{{item.AMOUNT}}</view>
							</u-col>
						</u-row>
					</view>
					<!-- <view class="apply-list-foot" slot="foot" :style="overStyle(item.over)">
						{{item.over? "入库完成":"未入库"}}
					</view> -->

				</u-card>
				<u-loadmore :status="status" />
			</view>


			<view>
				<!-- 提示窗示例 -->
				<uni-popup ref="alertDialog" type="dialog">
					<uni-popup-dialog :type="msgType"
					cancelText="关闭"
					confirmText="重新托盘回库"
					title="入库失败"
						:content="errContent"
						@confirm="dialogConfirm"
						@close="dialogClose"></uni-popup-dialog>
				</uni-popup>
			</view>
			<view style="height: 400rpx;">

			</view>


		</view>
		<u-row  class="more-bottom-box" justify="center">
			
			<u-col span="10">
				<view><u-button class="more-button" type="primary"
				shape="circle" @click="submitTpIn">托盘回库</u-button>
				</view>
			</u-col>
		</u-row>
	</view>
</template>
<script>
	export default {
		components: {

		},

		data() {
			return {
				status: 'nomore',
				hzList:[],
				// 给一个初始值
				scanInput: '',
				scanResult: '',
				msgType: 'success',
				dialog: '',
				notChangeQuyNo:true,
				form: {
					kuf_no: '',
					quy_no: '',
					gztid: '',
					tpNum: '', // DHJCK01
					//1：托盘回库2：空托出库
					czlx: '1',
					landpoint: '',// LSK000000004
					tpbar:'',
					// 卷数
					iv_nls:'1',
				},
				errContent: '未知错误,请重试',
				//库房
				kufList: [],
				//区域
				quNoList: [],
				//库别
				kubList: [],
				//工作台
				gztList: [],
				rangeNum: [{
						value: 1,
						text: "1"
					},
					{
						value: 2,
						text: "2"
					},
					{
						value: 3,
						text: "3"
					},
					{
						value: 4,
						text: "4"
					},
					{
						value: 5,
						text: "5"
					},
					{
						value: 6,
						text: "6"
					},
					{
						value: 7,
						text: "7"
					},
					{
						value: 8,
						text: "8"
					},
					{
						value: 9,
						text: "9"
					},
					{
						value: 10,
						text: "10"
					}
				],
				mxList: [],
			}
		},
		onUnload() {
			// 移除监听事件
			uni.$off('scancodedate')
		},
		onLoad() {
			//激光扫描监听事件
			var _this = this
			uni.$on("scancodedate", async (res) => {
				console.log("托盘回库页面监听到的async:" + res); //我是全局事件订阅的调用方法
				this.scanResult = JSON.stringify(res);
				setTimeout(() => {
					this.changeScanValue()
				}, 20);


			})

			this.form.kuf_no = uni.getStorageSync("in-emput-try-kuf_no")
			this.getKufbyKuf()
			setTimeout(()=>{
				this.form.quy_no = uni.getStorageSync("in-emput-try-quy_no")
				// this.form.quy_no ="直接是缓存中的值"
			},1000)
			this.form.tpNum=uni.getStorageSync("in-emput-try-landpoint")

		},
		created() {
			// this.getKuf(2)
			// this.getKuf(3)
			// this.getGzt()
		},
		// 数据变化适用
		onShow() {

		},
		methods: {
			// 判定点击的哪个输入框
			changeScanInput(inputItem) {
				console.log(inputItem)
				this.scanInput = inputItem
				//this.$u.toast(JSON.stringify(inputItem))
			},
			// 扫描后执行的方法,判断输入框并绑定值,可以自行跳转,或者扫描完执行某方法,
			changeScanValue() {
				let scanInput = this.scanInput
				let result = JSON.parse(this.scanResult)
				if ("form.landpoint" === scanInput) {
					console.log("this.form.landpoint")
					this.form.landpoint = result
				} else if("form.tpbar" === scanInput){
					this.form.tpbar = result
					this.searchHz()
				} else if("form.tpNum" === scanInput){
					this.form.tpNum = result
				}else if("form.iv_nls" === scanInput){
					this.form.iv_nls = result
					
				}else {
					// this.$u.toast('没有预先适配:' + scanInput)
					console.log('没有预先适配:' + scanInput)
				}
			},
			goTo(item, index) {
				if (item.over) {
					this.$u.toast('已完成')
					return
				} else {
					// let item = this.hzList[index]
					// this.oneMx = item
					// this.change(1)
				}


			},
			//失去焦点
			blurFunction(){
				this.updageFrom()
			},
			// 更新from
			updageFrom(){
				 uni.setStorageSync("in-emput-try-landpoint",this.form.tpNum)
				console.log("点位码缓存:"+uni.getStorageSync("in-emput-try-landpoint"))
				// uni.setStorageSync("in-emput-try-tpbar",this.form.tpbar)
				// console.log("点位码缓存:"+uni.getStorageSync("in-emput-try-tpbar"))
			},
			// 将区域可选
			canChangeQuy_no(){
				this.notChangeQuyNo = false

			},
			dialogConfirm() {
				this.submitTpIn()
			},
			dialogClose() {
				console.log('点击关闭')
			},
			change(e) {
				console.log("e:" + e);
			},
			changeKuf(e) {
				console.log("e:" + e);
				// const item = this.kufList[e];
				// console.log("item:"+item)
				this.form.kuf_no = e
				// 判断一下区域是不是在这个库房,如果在,就不更改区域,如果不在,就更改区域
				this.getKufbyKuf()
				this.notChangeQuyNo = e ? false : true

				uni.setStorageSync("in-emput-try-kuf_no",e)
				console.log("呼叫托盘缓存库房号:"+uni.getStorageSync("in-emput-try-kuf_no"))

			},
			changeQuyNo(e) {
				this.form.quy_no = e
				uni.setStorageSync("in-emput-try-quy_no",e)
				console.log("呼叫托盘缓存区域号:"+uni.getStorageSync("in-emput-try-quy_no"))
			},
			changeGztId(e) {
				console.log("changeGztId    e:" + e);
				this.form.gztid = e
			},
			changeTpNum(e) {
				this.form.tpNum = e
				this.updageFrom()
			},
			// 查看选择到的库房的区域
			getKufbyKuf() {
				const USER_ID = uni.getStorageSync('USER_ID')
				const KUB = '%'
				const KUF_NO = this.form.kuf_no ? this.form.kuf_no : "%"
				//1获取库别，2获取库房，3获取区域
				const GET_TYPE = 3
				let param = USER_ID + "," + KUB + "," + KUF_NO + "," + GET_TYPE;
				console.log(param)
				this.$u.api.general({
						iv_code: "GETKUFQUYNO",
						iv_parm: param
					})
					.then(res => {
						console.log("返回的res:" + JSON.stringify(res))
						let data = res.data
						console.log("查看选择到的库房的区域返回的data:" + JSON.stringify(data))
						if (res.success) {
							console.log("hello")
							//补充text
							let newData = data.filter((item) => {
								// 过滤如果有对象的区域参数和已经够选择的区域no相同,那么就不用覆盖已经选择的
								return item.QUY_NO === this.form.quy_no
							})
							if(newData.length>0){
								console.log("不用重新选择,这是从对象集合种筛出来的区域号相同的对象=>"+JSON.stringify(newData))
							}else{
								console.log("重新选择,这是库房对应的对象集合,=>"+JSON.stringify(data))
								this.quNoList=data.map(v => {
									this.$set(v, 'text', v.QUY_NO)
									this.$set(v, 'value', v.QUY_NO)
									return v
								})
								//如果切换的区域
								this.form.quy_no=''
							}
							this.canChangeQuy_no()




						} else {
							this.errContent = res.msg
							this.$u.toast(res.msg);
						}
					});
			},
			getKuf(type) {
				console.log("6666666666666")
				const USER_ID = uni.getStorageSync('USER_ID')
				const KUB = '%'
				const KUF_NO = "%"
				//1获取库别，2获取库房，3获取区域
				const GET_TYPE = type
				let param = USER_ID + "," + KUB + "," + KUF_NO + "," + GET_TYPE;
				console.log(param)
				this.$u.api.general({
						iv_code: "GETKUFQUYNO",
						iv_parm: param
					})
					.then(res => {
						// console.log("返回的res:" + JSON.stringify(res))
						let data = res.data
						console.log("kuf返回的data:" + JSON.stringify(data))
						if (res.success) {
							//补充text
							// this.kufList = data

							if (type == "2") {
								this.kufList = data.map(v => {
									this.$set(v, 'text', v.KUF_NO)
									this.$set(v, 'value', v.KUF_NO)
									return v
								})
							} else if (type == "3") {
								this.quNoList = data.map(v => {
									this.$set(v, 'text', v.QUY_NO)
									this.$set(v, 'value', v.QUY_NO)
									return v
								})
							} else {
								this.kubList = data.map(v => {
									this.$set(v, 'text', v.KUB)
									this.$set(v, 'value', v.KUB)
									return v
								})
							}

						} else {
							this.errContent = res.msg
							this.$u.toast(res.msg);
						}
					});
			},
			getGzt() {
				const user_id = uni.getStorageSync('USER_ID')
				const STORAGE_ID = uni.getStorageSync('STORAGE_ID')
				const kuf = this.form.kuf_no ? this.form.kuf_no : "%"
				const gztType = "1"
				const gztId = "%"
				let param = user_id + "," + STORAGE_ID + "," + kuf + "," + gztType + "," + gztId;
				console.log(param)
				this.$u.api.general({
						iv_code: "GETGZTINFO",
						iv_parm: param
					})
					.then(res => {
						// console.log("返回的res:" + JSON.stringify(res))
						let data = res.data
						console.log("返回的data:" + JSON.stringify(data))
						if (res.success) {
							//补充text
							this.gztList = data.map(v => {
								this.$set(v, 'text', v
									.GZTNAME
								) // https://vuejs.org/v2/guide/reactivity.html
								this.$set(v, 'value', v.GZTID)
								return v
							})
						} else {
							this.$u.toast(res.msg);
						}
					});
			},
			searchHz(){
				this.getHzList()
			},
			// 获取列表
			getHzList() {
				// 1、IV\_USER\_ID 用户编号,
				// 2、IV\_HUOJNO 货架号
				// 3、IV\_ORIENTATIONS 货架朝面
				const USER_ID = uni.getStorageSync('USER_ID')
				// LSK000000004
				const HUOJNO = this.form.tpbar ? this.form.tpbar : "%"
				const ORIENTATIONS = '0'


				let param = USER_ID + "," + HUOJNO + "," + ORIENTATIONS;

				console.log("iv_parm:" + param)
				let that = this
				this.$u.api.general({
						iv_code: "GETSTOCKHJ",
						iv_parm: param
					})
					.then(res => {
						// console.log("返回的res:" + JSON.stringify(res))
						let data = res.data
						console.log("返回的data:" + JSON.stringify(data))
						if (res.success) {
							//补充text
							this.hzList = data
							if (this.hzList.length < 1) {
								this.$u.toast("暂无数据");
							}
							this.status = 'nomore';

						} else {
							this.$u.toast(res.msg);
						}
					});
			},
			submitTpIn() {
				// 1、IV_GZTID 工作站
				// 2、IV_TPBAR 托盘号
				// 3、IV_STAFF 用户id
				// 4、IV_LANDPOINT 点位 
			
				const gztid = ""
				const tpbar=this.form.tpbar ? this.form.tpbar : ''
				if(tpbar===''){
					this.$u.toast('请填写托盘号')
					return;
				}
				const IV_STAFF = uni.getStorageSync('USER_ID')
				const landpoint = this.form.landpoint ? this.form.landpoint : ''
				if(landpoint===''){
					this.$u.toast('请填写点位码')
					return;
				}
				let param = gztid + "," + tpbar + "," + IV_STAFF + "," + landpoint  ;
				console.log("iv_parm:" + param + ";")
				let that = this
				this.$u.api.prcutlexecvar({
						iv_code: "PDATPBACK",
						iv_parm: param, 
						// form.iv_nls
						ov_var1: '2',
						ov_var2: '2',
						ov_var3: '2',
						ov_var4: '2',
						ov_var5: '2',
					})
					.then(res => {
						console.log("返回的res:" + JSON.stringify(res))
						if (res.success) {
							let data = res.data
							console.log("返回的data:" + JSON.stringify(data))
							console.log("返回的res.msg:" + res.msg)
							that.$u.toast(res.msg);
						} else {
							this.errContent = res.msg
							this.dialogToggle('error')
						}
			
					});
			},
			// submitTpIn() {
			// 	// 1、IV\_STORAGEID 仓库编号,
			// 	// 2、IV\_GZTID 工作台
			// 	// 3、IV\_USER\_ID 用户编号
			// 	// 4、IV\_INPAGENO 入库单号
			// 	// 5、IV\_ITM 行号
			// 	// 6、IV\_BATCHNO 批号
			// 	// 7、IV\_PRODUCTDATE 生产日期
			// 	// 8、IV\_EFFDATE 有效期
			// 	// 9、IV\_PRODUCTDATE\_PRINT 打印生产日期
			// 	// 10、IV\_EFFDATE\_PRINT 打印有效日期
			// 	// 11、IN\_AMOUNT 数量
			// 	// 12、IV\_TPBAR 托盘号
			// 	// 13、IV\_LOCATION 指定货位
			// 	// 14、IV\_GOODSTATUS 质量状态
			// 	// 15、IV\_KUB 库别
			// 	// 16、IV\_KUF\_NO 库房
			// 	// 17、IV\_QUY\_NO 区域
			// 	// 18、IV\_UNIQUECODE 唯一码
			// 	// 19、IV\_BGZID 保管账：默认1
			// 	// 20、IV\_NLS 内联数
			// 	// 21、IV\_YWZT 业务状态
			// 	// 22、IV_LANDPOINT 入库点
				
			// 	const STORAGE_ID = uni.getStorageSync('STORAGE_ID')
			// 	const GZTID =  "%"
			// 	const USER_ID = uni.getStorageSync('USER_ID')
			// 	const inbillNo =  "%"
			// 	console.log("单号:" + inbillNo)
			// 	const ITM = "%"
			// 	const BATCHNO =  "%"
				
			// 	const PRODUCTDATE = "%"
			// 	const EFFDATE ="%"
			// 	const PRODUCTDATE_PRINT = PRODUCTDATE
			// 	const EFFDATE_PRINT = EFFDATE
				
			// 	//200
			// 	const sjAmouont = ""
			// 	// if (!sjAmouont) {
			// 	// 	this.$u.toast('请填写上架数量')
			// 	// 	return;
			// 	// }
			// 	//本次入库数量上限
			// 	// const MaxAmount = Number(this.oneMx.AMOUNT - this.oneMx.SHIS_AMOUNT)
			// 	// if (Number(MaxAmount - this.sjAmouont) < 0) {
			// 	// 	this.$u.toast('超出本明细数量上限! 数量上限:' + MaxAmount)
			// 	// 	return;
			// 	// }
			// 	// console.log("数量上限:" + MaxAmount)
			// 	//0
			// 	const TPBAR = this.form.tpbar ? this.form.tpbar : ''
			// 	if(TPBAR===''){
			// 		this.$u.toast('请填写托盘号')
			// 		return;
			// 	}
			// 	// if (!TPBAR) {
			// 	// 	this.$u.toast('请填写托盘号')
			// 	// 	return;
			// 	// }
			// 	//空  huojItem.location
			// 	// const sjLocation = this.sjLocation ? this.sjLocation : ""
			// 	const sjLocation =  ""
			// 	// if (!sjLocation) {
			// 	// 	this.$u.toast('请填写或扫描货位')
			// 	// 	return;
			// 	// }
			// 	//1
			// 	const GOODSTATUS = 1
			// 	//%
			// 	const KUB =  "%"
			// 	// YCQ
			// 	const KUF_NO = "%"
			// 	//%
			// 	const QUY_NO =  "%"
			// 	//0
			// 	const UNIQUECODE = "0"
			// 	//1
			// 	const BGZID = "1"
			// 	//1
			// 	const NLS = this.form.iv_nls ? this.form.iv_nls : "1"
			// 	//01
			// 	const YWZT = "01"
			// 	//DHJ01
			// 	const landpoint = this.form.landpoint ? this.form.landpoint : ''
			// 	if(landpoint===''){
			// 		this.$u.toast('请填写点位码')
			// 		return;
			// 	}
			// 	// if (!landpoint || landpoint === "%") {
			// 	// 	this.$u.toast('请选择入库点')
			// 	// 	return;
			// 	// }
				
			// 	let param = STORAGE_ID + "," +
			// 		GZTID + "," +
			// 		USER_ID + "," +
			// 		inbillNo + "," +
			// 		ITM + "," +
			// 		BATCHNO + ","
				
			// 		+
			// 		PRODUCTDATE + "," +
			// 		EFFDATE + "," +
			// 		PRODUCTDATE_PRINT + "," +
			// 		EFFDATE_PRINT + ","
				
			// 		+
			// 		sjAmouont + "," +
			// 		TPBAR + "," +
			// 		sjLocation + "," +
			// 		GOODSTATUS + ","
				
			// 		+
			// 		KUB + "," +
			// 		KUF_NO + "," +
			// 		QUY_NO + ","
				
			// 		+
			// 		UNIQUECODE + "," +
			// 		BGZID + "," +
			// 		NLS + "," +
			// 		YWZT + "," +
			// 		landpoint;
				
			// 	console.log("iv_parm:" + param + ";")
			// 	let that = this
				
			// 	// 1、IV_GZTID 工作站
			// 	// 2、IV_TPBAR 托盘号
			// 	// 3、IV_STAFF 用户id
			// 	// 4、IV_LANDPOINT 点位 

			// 	// const gztid = ""
			// 	// const tpbar=this.form.tpbar ? this.form.tpbar : ''
			// 	// if(tpbar===''){
			// 	// 	this.$u.toast('请填写托盘号')
			// 	// 	return;
			// 	// }
			// 	// const IV_STAFF = uni.getStorageSync('USER_ID')
			// 	// const landpoint = this.form.landpoint ? this.form.landpoint : ''
			// 	// if(landpoint===''){
			// 	// 	this.$u.toast('请填写点位码')
			// 	// 	return;
			// 	// }
			// 	// let param = gztid + "," + tpbar + "," + IV_STAFF + "," + landpoint  ;
			// 	// console.log("iv_parm:" + param + ";")
			// 	// let that = this
			// 	this.$u.api.prcutlexecvar({
			// 			iv_code: "PDAINBILLSH",
			// 			iv_parm: param, 
			// 			// form.iv_nls
			// 			ov_var1: '2',
			// 			ov_var2: '2',
			// 			ov_var3: '2',
			// 			ov_var4: '2',
			// 			ov_var5: '2',
			// 		})
			// 		.then(res => {
			// 			console.log("返回的res:" + JSON.stringify(res))
			// 			if (res.success) {
			// 				let data = res.data
			// 				console.log("返回的data:" + JSON.stringify(data))
			// 				console.log("返回的res.msg:" + res.msg)
			// 				that.$u.toast(res.msg);
			// 			} else {
			// 				this.errContent = res.msg
			// 				this.dialogToggle('error')
			// 			}

			// 		});
			// },
			dialogToggle(type) {
				this.msgType = type
				this.$refs.alertDialog.open()
			},
		}

	}
</script>
<style lang="scss" scoped>
	@import '../../common/index.scss';
	@import '../../common/base-change.scss';
	// 样式覆盖基础样式 挤压后,解决线条覆盖
	.wrap {
		.example-body{
			.u-form{
				.u-form-item{
					margin-bottom: -10rpx !important;
					margin-top:-10rpx !important
				}
			}
		}
	}
</style>
